<template>
  <div>
    <!-- 标题 -->
    <van-cell title="搜索历史" value="内容">
      <template>
        <van-icon @click="isDelete = true" v-if="!isDelete" name="delete-o" />
        <div v-else>
          <!-- <span @click="history = []" >全部删除</span> 错误：不能给父组件传过来的数据赋值 -->
          <!-- <span @click="history.splice(0)" >全部删除</span> splice(0),第一个参数为0，代表全部删除 -->
          <span @click="$emit('clear')" >全部删除</span>
          <span @click="isDelete = false" style="margin-left: 5px">完成</span>
        </div>
      </template>
    </van-cell>
    <!-- 搜索列表 -->
    <van-cell @click="onClickHistory" :title="item" v-for="item in history" :key="item">
      <template v-if="isDelete">
        <van-icon name="close" />
      </template>
    </van-cell>
  </div>
</template>

<script>
export default {
  props: {
    history: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDelete: false
    }
  },

  created () {

  },

  methods: {
    onClickHistory (value) {
      if (this.isDelete) {
        // 处于删除状态
        const index = this.history.indexOf(value) // indexOf() 会返回查找目标的索引值
        this.history.splice(index, 1)
      } else {
        // 搜索
        this.$emit('onSearch', value)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
